package com.example.springmvcproject.Configuration;

import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.spring.SaBeanInject;
import cn.dev33.satoken.spring.SaBeanRegister;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;


@Configuration
@EnableAutoConfiguration
public class BaseConfiguration {

    // 获取配置Bean (以代码的方式配置Sa-Token, 此配置会覆盖yml中的配置)
    @Bean
    @Primary
    public SaTokenConfig getSaTokenConfigPrimary() {
        SaTokenConfig config = new SaTokenConfig();
        config.setTokenName("token")            // token名称 (同时也是cookie名称)
                .setTimeout(30 * 24 * 60 * 60)          // token有效期，单位s 默认30天
                .setActivityTimeout(60 * 10)            // token临时有效期 (指定时间内无操作就视为token过期) 单位: 秒
                .setIsConcurrent(false)         // 是否允许同一账号并发登录 (为true时允许一起登录, 为false时新登录挤掉旧登录)
                .setTokenStyle("uuid")          // token风格
                .setIsLog(false)            // 是否输出操作日志
                .setIsReadHead(true)           //从Header中获取token
                .setTokenSessionCheckLogin(true)//检查
                .setIsReadCookie(false)
                .setIsReadBody(false);
        return config;
    }
    @Bean
    public SaBeanRegister getSaBeanRegister(){
        return new SaBeanRegister();
    }

    @Bean
    public SaBeanInject getSaBeanInject(@Autowired SaTokenConfig saTokenConfig){
        SaBeanInject saBeanInject = new SaBeanInject();
        saBeanInject.setConfig(saTokenConfig);
        return saBeanInject;
    }

    /**
     * 配置上传文件
     * @return
     */
    @Bean
    public CommonsMultipartResolver getCommonsMultipartResolver(){
        CommonsMultipartResolver commonsMultipartResolver=new CommonsMultipartResolver();
        commonsMultipartResolver.setMaxUploadSize(1024*1024*2);
        commonsMultipartResolver.setDefaultEncoding("UTF-8");
        return commonsMultipartResolver;
    }

}
